home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / prog / graphics / triang.c < prev    next >
C/C++ Source or Header  |  1994-08-05  |  2KB  |  142 lines

  1. #include <LEDA/plane.h>
  2. #include <LEDA/d_array.h>
  3. #include <LEDA/graph.h>
  4. #include <LEDA/window.h>
  5.  
  6. window W;
  7.  
  8. void TRIANG(list<point> L, GRAPH<point,int>& G)
  9.   if (L.length() < 3) return;
  10.  
  11.   list<point> CH;
  12.   list_item last;
  13.  
  14.   L.sort();  // sort points lexicographically
  15.  
  16.   d_array<point,node> V(nil);
  17.  
  18.  
  19.  
  20.   // initialize convex hull with first two points
  21.  
  22.   point p = L.pop();
  23.   V[p] = G.new_node(p);
  24.   CH.append(p);
  25.  
  26.   while (L.head() == p) L.pop();
  27.   point q = L.pop();
  28.   last = CH.append(q);
  29.   V[q] = G.new_node(q);
  30.  
  31.   G.new_edge(V[p],V[q]);
  32.  
  33.  
  34.   // scan remaining points
  35.  
  36.   forall(p,L)
  37.   {
  38.     if (p == CH[last]) continue;  // multiple point
  39.  
  40.     node v = G.new_node(p);
  41.  
  42.     V[p] = v;
  43.  
  44.     G.new_edge(v,V[CH[last]]);
  45.  
  46.     // compute upper tangent (p,up)
  47.  
  48.     list_item up = last;
  49.     list_item it = CH.cyclic_succ(up);
  50.  
  51.     while (left_turn(CH[it],CH[up],p))
  52.     { up = it;
  53.       it = CH.cyclic_succ(up);
  54.       G.new_edge(v,V[CH[up]]);
  55.      }
  56.  
  57.  
  58.     // compute lower tangent (p,low)
  59.  
  60.     list_item low = last;
  61.     it = CH.cyclic_pred(low);
  62.  
  63.     while (right_turn(CH[it],CH[low],p))
  64.     { low = it;
  65.       it = CH.cyclic_pred(low);
  66.       G.new_edge(v,V[CH[low]]);
  67.      }
  68.  
  69.  
  70.     // remove all points between up and low
  71.  
  72.     if (up != low)
  73.     { it = CH.cyclic_succ(low);
  74.  
  75.       while (it != up)
  76.       { CH.del(it);
  77.         it = CH.cyclic_succ(low);
  78.        }
  79.      }
  80.  
  81.     // insert new point
  82.  
  83.     last = CH.insert(p,low);
  84.  
  85.    }
  86.  
  87. }
  88.      
  89.  
  90.  
  91. main()
  92. {
  93.   //window W;
  94.   W.init(-100,100,-100);
  95.   W.set_node_width(5);
  96.  
  97.   int N = 500;
  98.  
  99.   panel P("triangulation");
  100.  
  101.   P.int_item("# points",N,1,2000);
  102.   int b1 = P.button("mouse");
  103.   int b2 = P.button("random");
  104.   int b3 = P.button("quit");
  105.  
  106.   for(;;)
  107.   { 
  108.     list<point> L;
  109.     point p,q;
  110.  
  111.     int but = P.open();
  112.  
  113.     W.clear();
  114.  
  115.     if (but == b1)
  116.       while (W >> p)
  117.       { W.draw_point(p,blue);
  118.         L.append(p);
  119.        }
  120.  
  121.     if (but == b2)
  122.       for(int i = 0; i<N; i++) 
  123.       { point p(random(-90,90),random(-90,90));
  124.         W.draw_point(p,blue);
  125.         L.append(p);
  126.        }
  127.  
  128.     if (but == b3) break;
  129.   
  130.     GRAPH<point,int> G;
  131.     TRIANG(L,G);
  132.   
  133.     edge e;
  134.     forall_edges(e,G)
  135.        W.draw_segment(G[source(e)],G[target(e)],violet);
  136.   }
  137.    
  138.  return 0;
  139. }
  140.  
  141.